class Solution:
    def countQuadruples(self, s1: str, s2: str) -> int:
        size1, size2 = len(s1), len(s2)

        min_idx = {}
        for i in range(size1):
            if s1[i] not in min_idx:
                min_idx[s1[i]] = i

        max_idx = {}
        for i in range(size2 - 1, -1, -1):
            if s2[i] not in max_idx:
                max_idx[s2[i]] = i

        min_val, min_num = 0, 0

        for a in range(26):
            ch = chr(a + 97)
            if ch not in min_idx or ch not in max_idx:
                continue

            res = min_idx[ch] - max_idx[ch]

            if res < min_val:
                min_val, min_num = res, 1
            elif res == min_val:
                min_num += 1

        return min_num


if __name__ == "__main__":
    print(Solution().countQuadruples(s1="abcd", s2="bccda"))  # 1
    print(Solution().countQuadruples(s1="ab", s2="cd"))  # 0
